package com.lihao.wanandroid.ui.mine

import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.lihao.jetpackcore.ext.nav
import com.lihao.jetpackcore.util.ToastUtil
import com.lihao.wanandroid.R
import com.lihao.wanandroid.app.base.BaseFragment
import com.lihao.wanandroid.app.ext.jumpByLogin
import com.lihao.wanandroid.app.ext.showMessage
import com.lihao.wanandroid.app.network.NetworkApi
import com.lihao.wanandroid.app.util.CacheUtils
import com.lihao.wanandroid.ui.viewmodel.MineViewModel
import kotlinx.android.synthetic.main.fragment_mine.*


/**
 * Created by lihao on 2020/5/19.
 * Describe: 我的Fragment
 */
class MineFragment : BaseFragment<MineViewModel>() {

    override fun layoutId() = R.layout.fragment_mine

    override fun initView(savedInstanceState: Bundle?) {
        setHasOptionsMenu(true)
        initTitle("昵称", isShowBack = false)
        (activity as? AppCompatActivity)?.setSupportActionBar(toolbar)
        rlUserInfo.setOnClickListener {
            if (!CacheUtils.isLogin()) {
                nav().navigate(R.id.action_to_loginFragment)
            }
        }
        tvMyIntegral.setOnClickListener {
            nav().jumpByLogin {
                // 我的积分
            }
        }
        tvMyCollect.setOnClickListener {
            nav().jumpByLogin {
                // 我的收藏
                it.navigate(R.id.action_mainFragment_to_collectFragment)
            }
        }
        tvMyShare.setOnClickListener {
            nav().jumpByLogin {
                // 我的分享
                it.navigate(R.id.action_mainFragment_to_shareFragment)
            }
        }
        tvBacklog.setOnClickListener {
            nav().jumpByLogin {
                // 待办清单
                it.navigate(R.id.action_mainFragment_to_backlogFragment)
            }
        }
        tvSetting.setOnClickListener {
            nav().jumpByLogin {
                // 设置
                it.navigate(R.id.action_mainFragment_to_settingFragment)
//                showMessage(message = "确定退出登录么？",
//                    positiveButtonText = "确定",
//                    negativeButtonText = "取消",
//                    positiveButtonAction = {
//                        NetworkApi.instance.cookieJar.clear()
//                        CacheUtils.setUserInfo(null)
//                        shareViewModel.userInfo.postValue(null)
//                    })
            }
        }

        // 显示个人信息
        CacheUtils.getUserInfo()?.let {
            Glide.with(this)
                .load("http://5b0988e595225.cdn.sohucs.com/q_70,c_zoom,w_640/images/20190115/87868f21befc4e7f9007aa71efa79621.jpeg")
                .placeholder(R.drawable.ic_default_photo)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .into(ivHeaderPhoto)
            tvUserName.text = it.nickname
            tvTitle?.text = it.nickname
            tvLevel.text = "Lv.100"
            tvSource.text = "积分: 0"
            tvRanking.text = "排行: 暂无"
        }
    }

    override fun lazyLoadData() {

    }

    override fun createObserver() {
        shareViewModel.userInfo.observe(this, Observer {

        })
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        inflater.inflate(R.menu.mine_menu, menu)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.item_notice) {
            ToastUtil.showShort(context, "点击了通知")
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onDestroyView() {
        super.onDestroyView()
    }
}